--------------------------------------------------------------------------------
-- Company: 
-- Engineer:
--
-- Create Date:   22:06:04 09/16/2012
-- Design Name:   
-- Module Name:   /home/tom/Electronics/sd_card/sd_uart_test_tb.vhd
-- Project Name:  sd_card
-- Target Device:  
-- Tool versions:  
-- Description:   
-- 
-- VHDL Test Bench Created by ISE for module: sd_uart_test
-- 
-- Dependencies:
-- 
-- Revision:
-- Revision 0.01 - File Created
-- Additional Comments:
--
-- Notes: 
-- This testbench has been automatically generated using types std_logic and
-- std_logic_vector for the ports of the unit under test.  Xilinx recommends
-- that these types always be used for the top-level I/O of a design in order
-- to guarantee that the testbench will bind correctly to the post-implementation 
-- simulation model.
--------------------------------------------------------------------------------
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
 
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--USE ieee.numeric_std.ALL;
 
ENTITY sd_uart_test_tb IS
END sd_uart_test_tb;
 
ARCHITECTURE behavior OF sd_uart_test_tb IS 
 
    -- Component Declaration for the Unit Under Test (UUT)
 
    COMPONENT sd_uart_test
    PORT(
         Clk_Ext : IN  std_logic;
         Reset : IN  std_logic;
         Rx : IN  std_logic;
         Tx : OUT  std_logic;
         Sd_Clk_Out : OUT  std_logic;
         Sd_Cmd : INOUT  std_logic;
         Sd_Data : INOUT  std_logic
        );
    END COMPONENT;
    

   --Inputs
   signal Clk_Ext : std_logic := '0';
   signal Reset : std_logic := '0';
   signal Uart_Rx : std_logic := '1';

	--BiDirs
   signal Sd_Cmd : std_logic;
   signal Sd_Data : std_logic;

 	--Outputs
   signal Uart_Tx : std_logic;
   signal Sd_Clk : std_logic;

   -- Clock period definitions
   constant Clk_Ext_period : time := 31 ns;
 
BEGIN
 
	-- Instantiate the Unit Under Test (UUT)
   uut: sd_uart_test PORT MAP (
          Clk_Ext => Clk_Ext,
          Reset => Reset,
          Rx => Uart_Rx,
          Tx => Uart_Tx,
          Sd_Clk_Out => Sd_Clk,
          Sd_Cmd => Sd_Cmd,
          Sd_Data => Sd_Data
        );
 
   -- Clock process definitions
   Clk_Ext_process :process
   begin
		Clk_Ext <= '0';
		wait for Clk_Ext_period/2;
		Clk_Ext <= '1';
		wait for Clk_Ext_period/2;
   end process;
 

   -- Stimulus process
   stim_proc: process
   begin		
		Sd_Cmd <= 'Z';
		
      -- hold reset state for 100 ns.
      wait for 1 ms;

		Uart_Rx <= '0';
		wait for 104 us;
		
		-- 00100000
		
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '1';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		
		Uart_Rx <= '1';
		
		
		
		
		
		wait for 2 ms;

		Uart_Rx <= '0';
		wait for 104 us;
		
		-- 00100000
		
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '1';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		
		Uart_Rx <= '1';
		
		wait for 2 ms;
		
		-- start bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- transmission bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- card index
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- reserved
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		
		-- voltage
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';

		-- echo back check pattern
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- CRC
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		
		-- end bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';

		-- high Z
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= 'Z';
		
		
		wait for 4 ms;
		
		Uart_Rx <= '0';
		wait for 104 us;
		
		-- 00100000
		
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '1';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		
		Uart_Rx <= '1';
		
		
		
		
				wait for 2 ms;
		
		-- start bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- transmission bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- card index
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- reserved
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		
		-- voltage
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';

		-- echo back check pattern
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- CRC
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		
		-- end bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';

		-- high Z
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= 'Z';
		
		
--      wait for Clk_Ext_period*1000;
--		
--		Uart_Rx <= '0';
--		wait for 104 us;
--		
--		-- 01100111
--		
--		Uart_Rx <= '1';
--		wait for 104 us;
--		Uart_Rx <= '1';
--		wait for 104 us;
--		Uart_Rx <= '1';
--		wait for 104 us;
--		Uart_Rx <= '0';
--		wait for 104 us;
--		Uart_Rx <= '0';
--		wait for 104 us;
--		Uart_Rx <= '1';
--		wait for 104 us;
--		Uart_Rx <= '1';
--		wait for 104 us;
--		Uart_Rx <= '0';
--		wait for 104 us;
--		
--		Uart_Rx <= '1';
--		wait for 104 us;
--		
--		Uart_Rx <= '1';
--		
--		wait for 1ms;
--		
--		Uart_Rx <= '0';
--		wait for 104 us;
--		
--		Uart_Rx <= '1';
		
--		-- 00100000
--		
--		Uart_Rx <= '0';
--		wait for 104 us;
--		Uart_Rx <= '0';
--		wait for 104 us;
--		Uart_Rx <= '0';
--		wait for 104 us;
--		Uart_Rx <= '0';
--		wait for 104 us;
--		Uart_Rx <= '0';
--		wait for 104 us;
--		Uart_Rx <= '1';
--		wait for 104 us;
--		Uart_Rx <= '0';
--		wait for 104 us;
--		Uart_Rx <= '0';
--		wait for 104 us;
--		
--		Uart_Rx <= '1';
--		wait for 104 us;

--		wait for 70 ms;
--		
--		-- start bit
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		
--		-- transmission bit
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		
--		-- card index
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '1';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		
--		-- reserved
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		
--		
--		-- voltage
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '1';
--
--		-- echo back check pattern
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '1';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '1';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '1';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '1';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		
--		-- CRC
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '1';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '1';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '1';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '1';
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '0';
--		wait until rising_edge(Sd_Clk);
--		
--		-- end bit
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= '1';
--
--		-- high Z
--		wait until rising_edge(Sd_Clk);
--		Sd_Cmd <= 'Z';

		wait for 200 us;
		
		Uart_Rx <= '0';
		wait for 104 us;
		
		-- 00100000
		
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '1';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		
		Uart_Rx <= '1';

		wait for 500 us;
		
		-- start bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- transmission bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- card index
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- reserved
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		
		-- voltage
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';

		-- echo back check pattern
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- CRC
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		
		-- end bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';

		-- high Z
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= 'Z';
		
		
		
		
		wait for 200 us;
		
		Uart_Rx <= '0';
		wait for 104 us;
		
		-- 00100000
		
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '1';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		
		Uart_Rx <= '1';

		wait for 500 us;
		
		-- start bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- transmission bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- card index
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		
		-- reserved
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		
		
		-- voltage
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';

		-- echo back check pattern
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		
		-- CRC
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		
		-- end bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';

		-- high Z
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= 'Z';
		
		
		
		wait for 200 us;
		
		Uart_Rx <= '0';
		wait for 104 us;
		
		-- 00100000
		
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '1';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		
		Uart_Rx <= '1';

		wait for 500 us;
		
		-- start bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- transmission bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- card index
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- reserved
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		
		-- voltage
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';

		-- echo back check pattern
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- CRC
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		
		-- end bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';

		-- high Z
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= 'Z';
		
		
		
		wait for 200 us;
		
		Uart_Rx <= '0';
		wait for 104 us;
		
		-- 00100000
		
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '1';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		
		Uart_Rx <= '1';

		wait for 500 us;
		
		-- start bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- transmission bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- card index
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- reserved
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		
		-- voltage
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';

		-- echo back check pattern
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- CRC
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		
		-- end bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';

		-- high Z
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= 'Z';
		
		
		
		wait for 200 us;
		
		Uart_Rx <= '0';
		wait for 104 us;
		
		-- 00100000
		
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '1';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		
		Uart_Rx <= '1';

		wait for 500 us;
		
		-- start bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- transmission bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- card index
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- reserved
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		
		-- voltage
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';

		-- echo back check pattern
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- CRC
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		
		-- end bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';

		-- high Z
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= 'Z';
		
		
		
		wait for 200 us;
		
		Uart_Rx <= '0';
		wait for 104 us;
		
		-- 00100000
		
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '1';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		
		Uart_Rx <= '1';

		wait for 500 us;
		
		-- start bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- transmission bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- card index
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- reserved
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		
		-- voltage
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';

		-- echo back check pattern
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- CRC
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		
		-- end bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';

		-- high Z
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= 'Z';
		
		
		
		wait for 200 us;
		
		Uart_Rx <= '0';
		wait for 104 us;
		
		-- 00100000
		
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '1';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		Uart_Rx <= '0';
		wait for 104 us;
		
		Uart_Rx <= '1';

		wait for 500 us;
		
		-- start bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- transmission bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- card index
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- reserved
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		
		-- voltage
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';

		-- echo back check pattern
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		
		-- CRC
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '0';
		wait until rising_edge(Sd_Clk);
		
		-- end bit
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= '1';

		-- high Z
		wait until rising_edge(Sd_Clk);
		Sd_Cmd <= 'Z';
		

      wait;
   end process;

END;
